package com.gt.utils;

import java.security.MessageDigest;

public class SHAUtil {
    /**
     * @Comment SHA1实现
     * @Author Ron
     * @Date 2017年9月13日 下午3:30:36
     * @return
     */
    public static String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
    public static void main(String args[]) throws Exception {
        String str = "noncestr=c35f3b60f3524b7c9ad3704e450d6f8d&jsapi_ticket=kgt8ON7yVITDhtdwci0qeUDZ2DhjffW3M6kxQAxkBS2SI96QuF0QvDhq8Q1wTkRmejdrZYiAYiVkqz2DspFwxA&timestamp=1631860892887&url=https://czxvasfile.postar.cn/h5/prod/activity_wxcoupon_11627/merchantList.html?content=wx313cfd5a1d78e709_2&openId=oUfGxwGYlT9tb9mfJY8WFPq7en4E&type=2&unionId=oH40YxLYDNO5KfWsI7a-cHB0rL5k&batchNo=15904465";
        System.out.println("原始：" + str);
        System.out.println("SHA后：" + shaEncode(str));
    }
}
